import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/model/nutrition_facts_model.dart';
import 'package:flutter_douban/tool_views/piechart/PieData.dart';
import 'package:flutter_douban/tool_views/piechart/my_custom_circle.dart';

class NutritionFactsView extends StatefulWidget {
  final List<NutritionFactsModel> dataArr;

  const NutritionFactsView({Key? key, required this.dataArr}) : super(key: key);

  @override
  _NutritionFactsViewState createState() => _NutritionFactsViewState();
}

class _NutritionFactsViewState extends State<NutritionFactsView> {
  ///数据源
  late List<PieData> mData;

  ///传递值
  late PieData pieData;
  int all = 0;

  ///初始化数据源
  void initData() {
    mData = [];

    for (var model in widget.dataArr) {
      all += model.calorie!;
    }

    for (var model in widget.dataArr) {
      PieData p1 = new PieData();
      p1.name = 'A';
      p1.price = 'a';
      p1.percentage = model.calorie! / all;
      p1.color = HexColor(model.pie_chart_color!);
      pieData = p1;
      mData.add(p1);
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initData();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Container(
          width: 70,
          height: 70,
          // color: Colors.red,
          child: MyCustomCircle('', 0, 30, 15, 35, mData, pieData),
        ),
        SizedBox(
          width: 5,
        ),
        Container(
          // color: Colors.red,
          height: 72,
          child: Column(
            children: [
              SizedBox(
                height: 5,
              ),
              _rowWidget(HexColor(widget.dataArr[0].pie_chart_color!),
                  widget.dataArr[0].nutrient!, widget.dataArr[0].amount!),
              _rowWidget(HexColor(widget.dataArr[1].pie_chart_color!),
                  widget.dataArr[1].nutrient!, widget.dataArr[1].amount!),
              _rowWidget(HexColor(widget.dataArr[2].pie_chart_color!),
                  widget.dataArr[2].nutrient!, widget.dataArr[2].amount!),
            ],
          ),
        ),
        Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Center(
              child: Container(
                width: 1,
                height: 40,
                color: AppColors.line,
              ),
            ),
            Column(
              children: [
                Text(
                  all.toString(),
                  style: TextStyle(fontSize: 16, color: Colors.black,fontWeight: FontWeight.bold),
                ),
                Text(
                  '千卡',
                  style: TextStyle(fontSize: 14, color: Colors.black54),
                ),
              ],
            )
          ],
        ))
      ],
    );
  }

  /// 营养分析
  Widget _rowWidget(Color color, String title, String count) {
    return Container(
      width: 135,
      padding: EdgeInsets.only(top: 2, bottom: 1),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          // _itemRow(color,title),
          Container(
            width: 10,
            height: 10,
            decoration: BoxDecoration(
              color: color,
              borderRadius: BorderRadius.circular(5),
            ),
          ),
          SizedBox(
            width: 15,
          ),
          Text(
            title,
            style: TextStyle(fontSize: 11, color: Colors.black54),
          ),
          Expanded(
            flex: 2,
            child: Text(
              count,
              textAlign: TextAlign.right,
              style: TextStyle(
                  fontSize: 13,
                  color: Colors.black,
                  fontWeight: FontWeight.bold),
            ),
          )
        ],
      ),
    );
  }
}
